﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace Sayeet
{
    public class XMPPMessage
    {
        //internal XDocument stanza;
        internal XElement stanza;
        internal static XNamespace ns;
        internal static XNamespace stream;
        internal static string Lang;
        internal static int MessageCount = 0;
        internal static XNamespace roster = "jabber:iq:roster";

        static XMPPMessage()
        {
            ns = "jabber:client";
            stream = "http://etherx.jabber.org/streams";
            Lang = "en";
        }

        public XMPPMessage()
        {
        }

        public XMPPMessage(XElement e)
        {
            stanza = e;
        }

        public void BuildMessage(string from, string to, string text)
        {
            stanza = new XElement("message",
                new XAttribute("from", from)//,
                //new XAttribute("to", to),
                //new XAttribute("xml:lang", Lang),
                //new XElement("body", text));
                );
        }

        public void BuildOpening(string address)
        {
            //StringBuilder sb = new StringBuilder();
            //sb.Append("<stream:stream ");
            //sb.Append(" to='" + address + "'");
            //sb.Append(" version='1.0'");
            //sb.Append(" xmlns='jabber:client'");
            //sb.Append(" xmlns:stream='http://etherx.jabber.org/streams' ");
            //sb.Append("/>");
            stanza = new XElement("stream",
                new XAttribute("to", address),
                new XAttribute("version", "1.0")
                );

        }

        public static XMPPMessage CreateChatMessage(string to, string from, string body)
        {
            XMPPMessage result = new XMPPMessage();
            result.stanza = new XElement(ns + "message",
                new XAttribute("type", "chat"),
                new XAttribute("id", "SayeetMsg" + MessageCount.ToString()),
                new XAttribute("to", to),
                new XAttribute("from", from),
                new XElement(ns + "body", body)
                );
            MessageCount++;

            return result;
        }

        public static XMPPMessage CreateResult(string to, string from, string id)
        {
            XMPPMessage result = new XMPPMessage();
            result.stanza = new XElement("iq",
                new XAttribute("from", from),
                new XAttribute("to", to),
                new XAttribute("type", "result"),
                new XAttribute("id", id)
                );
            return result;
        }

        public static XMPPMessage CreateRosterSet(string jid, string name, string group)
        {
            //<iq type='set' id='set1'>
            //  <query xmlns='jabber:iq:roster'>
            //    <item
            //        jid='contact@example.org'
            //        name='MyContact'>
            //      <group>MyBuddies</group>
            //    </item>
            //  </query>
            //</iq>
            XMPPMessage result = new XMPPMessage();
            result.stanza = new XElement("iq",
                new XAttribute("type", "set"),
                new XAttribute("id", "Sayeet_roster_" + MessageCount.ToString()),
                new XElement(roster + "query",
                    new XElement(roster + "item",
                        new XAttribute("jid", jid),
                        new XAttribute("name", name),
                        new XElement(roster + "group", group)
                        )
                    )
                );
            return result;
        }


        public static XMPPMessage CreateSubscriptionRequest(string to)
        {
            XMPPMessage result = new XMPPMessage();
            result.stanza = new XElement("presence",
                new XAttribute("to", to),
                new XAttribute("type", "subscribe")
                );
            return result;
        }

        public static XMPPMessage CreateSubscriptionResponse(string to, bool accept)
        {
            XMPPMessage result = new XMPPMessage();
            result.stanza = new XElement("presence",
                new XAttribute("to", to),
                new XAttribute("type", accept ? "subscribed" : "unsubscribed")
                );
            return result;
        }

        public static XMPPMessage CreateSubscriptionCancel(string to)
        {
            XMPPMessage result = new XMPPMessage();
            result.stanza = new XElement("presence",
                new XAttribute("to", to),
                new XAttribute("type", "unsubscribed")
                );
            return result;
        }

        public static XMPPMessage CreateSubscriptionUnsubscribe(string to)
        {
            XMPPMessage result = new XMPPMessage();
            result.stanza = new XElement("presence",
                new XAttribute("to", to),
                new XAttribute("type", "unsubscribe")
                );
            return result;
        }

        public static XMPPMessage CreateRosterDelete(string from, string jid)
        {
            XMPPMessage result = new XMPPMessage();
            
            result.stanza = new XElement("iq",
                new XAttribute("from", from),
                new XAttribute("type", "set"),
                new XAttribute("id", "Sayeet_roster_" + MessageCount),
                new XElement(roster + "query",
                    new XElement(roster + "item",
                        new XAttribute("jid", jid),
                        new XAttribute("subscription", "remove")
                        )
                    )
                );
            return result;

        }

        public override string ToString()
        {
            return stanza.ToString();
        }
    }
}
